{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generators and Context Managers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's see how we might write something that almost behaves like a context manager, using a generator function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def my_gen():\n",
    "    try:\n",
    "        print('creating context and yielding object')\n",
    "        lst = [1, 2, 3, 4, 5]\n",
    "        yield lst\n",
    "    finally:\n",
    "        print('exiting context and cleaning up')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "gen = my_gen()  # create generator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "creating context and yielding object\n"
     ]
    }
   ],
   "source": [
    "lst = next(gen)  # enter context and get \"as\" object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "exiting context and cleaning up\n"
     ]
    },
    {
     "ename": "StopIteration",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mStopIteration\u001b[0m                             Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-5-ad32aeec95bc>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgen\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m# exit context\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mStopIteration\u001b[0m: "
     ]
    }
   ],
   "source": [
    "next(gen)  # exit context"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see, the exiting context code ran.\n",
    "But to make this cleaner, we'll catch the StopIteration exception:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "creating context and yielding object\n",
      "[1, 2, 3, 4, 5]\n",
      "exiting context and cleaning up\n"
     ]
    }
   ],
   "source": [
    "gen = my_gen()\n",
    "lst = next(gen)\n",
    "print(lst)\n",
    "try:\n",
    "    next(gen)\n",
    "except StopIteration:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's write a context manager that can use the type of generator we wrote so we can use it using a `with` statement instead:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class GenCtxManager:\n",
    "    def __init__(self, gen_func):\n",
    "        self._gen = gen_func()\n",
    "        \n",
    "    def __enter__(self):\n",
    "        return next(self._gen)\n",
    "    \n",
    "    def __exit__(self, exc_type, exc_value, exc_tb):\n",
    "        try:\n",
    "            next(self._gen)\n",
    "        except StopIteration:\n",
    "            pass\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "creating context and yielding object\n",
      "[1, 2, 3, 4, 5]\n",
      "exiting context and cleaning up\n"
     ]
    }
   ],
   "source": [
    "with GenCtxManager(my_gen) as lst:\n",
    "    print(lst)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Our `GenCtxManager` class is not very flexible - we cannot pass arguments to the generator function for example. We are also not doing any exception handling..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We could try some of this ourselves. \n",
    "For example handling arguments:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class GenCtxManager:\n",
    "    def __init__(self, gen_func, *args, **kwargs):\n",
    "        self._gen = gen_func(*args, **kwargs)\n",
    "        \n",
    "    def __enter__(self):\n",
    "        return next(self._gen)\n",
    "    \n",
    "    def __exit__(self, exc_type, exc_value, exc_tb):\n",
    "        try:\n",
    "            next(self._gen)\n",
    "        except StopIteration:\n",
    "            pass\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def open_file(fname, mode):\n",
    "    try:\n",
    "        print('opening file...')\n",
    "        f = open(fname, mode)\n",
    "        yield f\n",
    "    finally:\n",
    "        print('closing file...')\n",
    "        f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "opening file...\n",
      "writing to file...\n",
      "closing file...\n"
     ]
    }
   ],
   "source": [
    "with GenCtxManager(open_file, 'test.txt', 'w') as f:\n",
    "    print('writing to file...')\n",
    "    f.write('testing...')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "testing...\n"
     ]
    }
   ],
   "source": [
    "with open('test.txt') as f:\n",
    "    print(next(f))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This works, but is not very elegant, and we still are not doing much exception handling. \n",
    "In the next video, we'll look at a decorator in the standard library that does this far more robustly and elegantly..."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
